package com.example.genericrpg_android;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.widget.FrameLayout;

public class GameActivity extends Activity {
	private FrameLayout frame;
	private GameView gameView;
	private int threadSpeed;
	private boolean init = false;
	
	private Handler handler = new Handler();
	private Runnable runnable = new Runnable() {
		public void run() {
			if (!init){
				gameView.initialize();
				init = true;
			}
			handler.postDelayed(runnable, threadSpeed);
			gameView.updateAll();
		}
	};
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        
        frame = (FrameLayout)findViewById(R.id.gameFrame);
        gameView = new GameView(this);
        frame.addView(gameView);
        threadSpeed = 10;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_game, menu);
        return true;
    }
    
    protected void onPause() {
    	handler.removeCallbacks(runnable);
    	gameView.pauseAll();
    	super.onPause();
    }
    
    @Override
	protected void onStop() {
    	handler.removeCallbacks(runnable);
    	gameView.pauseAll();
		super.onStop();
	}

	protected void onResume() {
    	handler.postDelayed(runnable, threadSpeed);
    	gameView.resumeAll();
    	super.onPause();
    }
    
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
      // ignore orientation/keyboard change
      super.onConfigurationChanged(newConfig);
    }

}
